package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /***
     * 用户登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
//        ①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
//        ②. 根据页面提交的用户名username查询数据库中员工数据信息
        Employee emp = employeeService.login(employee.getUsername(), password);
//        ③. 如果没有查询到, 则返回登录失败结果
        if (emp == null) {
            return R.error("登录失败，用户名或者密码错误");
        }
//        ⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账户已禁用,请联系管理员");
        }
//        ⑥. 登录成功，将员工id存入Session, 并返回登录成功结果
        request.getSession().setAttribute("emp", emp.getId());
        return R.success(emp);

    }

    /****
     * 用户退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        session.invalidate(); //销毁session
        return R.success("退出成功");
    }

    /****
     * 添加用户
     * @param session
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpSession session, @RequestBody Employee employee) {
        log.info("添加的用户信息 {}", employee.toString());
        //1.给用户设置初始密码,进行加密
        //employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //2.获取当前用户id
       /* Long id = (Long) session.getAttribute("emp");
        employee.setCreateUser(id);
        employee.setUpdateUser(id);*/

        //3.添加用户
        boolean flag = employeeService.save(employee);
        //4.返回结果
        return flag ? R.success("添加员工成功") : R.error("添加员工失败");
    }

    /****
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> findByPageAndConditions(int page, int pageSize, String name) {
        log.info("page = {},pagesize = {},name = {}", page, pageSize, name);
        //查询
        Page<Employee> p = employeeService.findByPageAndConditions(page, pageSize, name);
        return p.getRecords() != null ? R.success(p) : R.error("暂无信息");
    }

    /*******
     * 信息修改
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        log.info("修改的用户信息{}" ,employee.toString());
        //1.获取当前用户信息
        //Long empId = (Long) session.getAttribute("emp");
       // employee.setUpdateUser(empId);//设置修改信息
        //2.修改
        boolean flag = employeeService.update(employee);
        //3.返回结果
        return flag ?  R.success("信息修改成功!") : R.error("信息修改失败!");
    }


    /*****
     * 根据id查询结果回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
         log.info("查询的id {}",id);
         Employee employee = employeeService.getById(id);
         return employee != null ? R.success(employee) : R.error("没有查询到数据");
    }

}
